package com.最大不重复子串;

import java.util.Arrays;
import java.util.HashSet;

class Solution {
	public int lengthOfLongestSubstring(String s) {
		HashSet hs = new HashSet();
		char c[] = s.toCharArray();
		int i = 0;
		int max = 0;
		int count = 0;
		int j = 0;
		for (i = 0; i < c.length; i++) {
			for (j = i; j < c.length; j++) {
				if (hs.add(c[j])) {
					count++;
					if (max < count)
						max = count;
				} else {
					if (max < count) {
						max = count;
					}
					count = 0;
					hs.clear();
					j--;
					break;
				}
			}
		}
		return max;
	}
	
	 public int lengthOfLongestSubstringBetter(String s) {
	        HashSet hs = new HashSet();
	        int i=0,left=0,max=0;
	        while(i<s.length()){
	            if(hs.add(s.charAt(i))){
	                max=Math.max(max,hs.size());
	                i++;
	            }else{
	                hs.remove(s.charAt(left++));
	            }
	        }
	        return max;
	    }
}
